package com.tianqu.tms.core.base;

import org.apache.log4j.PropertyConfigurator;

/**
 * A Logger is a GWA standard compliance format logger.
 */
public class Logger {
	private final static String SEVERITY_1 = "[Sev1] ";
	private final static String SEVERITY_2 = "[Sev2] ";
	private final static String SEVERITY_3 = "[Sev3] ";
	private final static String SEVERITY_4 = "[Sev4] ";
	
	private final static String ROOT = "ROOT";
	private final static String APPOUT = "appout.";
	private static final String APPERR = "apperr.";
	
	protected static Logger rootLogger = null ;

	/**
	 * 
	 * @return
	 */
	public static synchronized Logger getRootLogger() {
		if (rootLogger == null){
			rootLogger = getLogger(ROOT);
		}
		return rootLogger;
	}

	/**
	 * Logs DEBUG level message to anonymouse logger categorized "ROOT".<br>
	 */
	public static void out(Object message) {
		getRootLogger().debug(message);
	}
	/**
	 * Logs DEBUG level message to anonymouse logger categorized "ROOT".<br>
	 */
	public static void out(Object message, Throwable t) {
		getRootLogger().debug(message, t);
	}

	/**
	 * Retrieves a logger by class name.<br>
	 */
	public static Logger getLogger(Class clazz) {
		return getLogger(clazz.getName());
	}

	/**
	 * Retrieves a logger by name.
	 */
	public static Logger getLogger(String category) {
		Logger logger = new Logger(category);
		return logger;
	}

	private org.apache.log4j.Logger appout;
	private org.apache.log4j.Logger apperr;
	/**
	 *@param name String
	 *@return Logger
	 */
	protected Logger(String name) {
		appout = org.apache.log4j.Logger.getLogger(APPOUT + name);
		apperr = org.apache.log4j.Logger.getLogger(APPERR + name);
	}
	/**
	 *@param out
	 *@param err
	 *@return Logger
	 */
	protected Logger(
		org.apache.log4j.Logger out,
		org.apache.log4j.Logger err) {
		appout = out;
		apperr = err;
	}
	/**
	 * isTraceEnabled
	 *@return boolean
	 */
	public boolean isTraceEnabled() {
		return appout.isTraceEnabled();
	}
	/**
	 * isDebugEnabled
	 *@return boolean
	 */
	public boolean isDebugEnabled() {
		return appout.isDebugEnabled();
	}
	/**
	 * isInfoEnabled
	 *@return boolean
	 */
	public boolean isInfoEnabled() {
		return appout.isInfoEnabled();
	}
	/**
	 * trace
	 *@param message Object
	 */
	public void trace(Object message) {
		appout.trace(SEVERITY_4 + message);
	}
	/**
	 * trace
	 *@param message Object
	 *@param t Throwable
	 */
	public void trace(Object message, Throwable t) {
		appout.trace(SEVERITY_4 + message, t);
	}
	/**
	 * debug
	 *@param message Object
	 */
	public void debug(Object message) {
		appout.debug(SEVERITY_4 + message);
	}
	/**
	 * debug
	 *@param message Object
	 *@param t Throwable
	 */
	public void debug(Object message, Throwable t) {
		appout.debug(SEVERITY_4 + message, t);
	}
	/**
	 * info
	 *@param message Object
	 */
	public void info(Object message) {
		appout.info(SEVERITY_4 + message);
	}
	/**
	 * info
	 *@param message Object
	 *@param t Throwable
	 */
	public void info(Object message, Throwable t) {
		appout.info(SEVERITY_4 + message, t);
	}
	/**
	 * warn
	 *@param message Object
	 */
	public void warn(Object message) {
		apperr.warn(SEVERITY_3 + message);
	}
	/**
	 * warn
	 *@param message Object
	 *@param t Throwable
	 */
	public void warn(Object message, Throwable t) {
		apperr.warn(SEVERITY_3 + message, t);
	}
	/**
	 * error
	 *@param message Object
	 */
	public void error(Object message) {
		apperr.error(SEVERITY_2 + message);
	}
	/**
	 * error
	 *@param message Object
	 *@param t Throwable
	 */	
	public void error(Object message, Throwable t) {
		apperr.error(SEVERITY_2 + message, t);
	}
	/**
	 * fatal
	 *@param message Object
	 */
	public void fatal(Object message) {
		apperr.fatal(SEVERITY_1 + message);
	}
	/**
	 * fatal
	 *@param message Object
	 *@param t Throwable
	 */	
	public void fatal(Object message, Throwable t) {
		apperr.fatal(SEVERITY_1 + message, t);
	}

	/**
	 * sample test application<br>
	 * java com.ibm.amsjp.log4gwa.Logger
	 * @param args String[]
	 */
	public static void main(String args[]) {
		Logger logger = Logger.getLogger(Logger.class);

		logger.trace("trace message");
		logger.debug("Debug Message");
		logger.info("Information Message");
		logger.warn("Warning Message");
		logger.error("error message");
		logger.fatal("fatal mes");
		logger.error(
			"error exception stacktrace",
			new Exception("!Exception!"));

		Logger.out("instant debug message");
	}

}
